package com.huanletao.threadPool;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

public class demo {
    AtomicInteger atomicInteger = new AtomicInteger();

    private int index;

    public void increase() throws InterruptedException {
        new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                index = atomicInteger.incrementAndGet();
            }
        }).start();

        new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                index = atomicInteger.incrementAndGet();
            }
        }).start();

        Thread.sleep(1000);
        System.out.println("-----" + index); // 输出结果20000
    }


    public static void main(String[] args) throws InterruptedException {


        AtomicReference<Book> atomicReference = new AtomicReference<>();
        Book book1 = new Book("三国演义", 42);
        atomicReference.set(book1);
        Book book2 = new Book("水浒传", 40);
        atomicReference.compareAndSet(book1, book2);
        System.out.println("Book name is " + atomicReference.get().name + ",价格是" + atomicReference.get().price);



    }
}
